//
//  YFRefreshView.swift
//  刷新控件
//
//  Created by tsaievan on 2017/9/13.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit

///< 定义一个枚举, 表示刷新的三种状态
enum YFRefreshState {
    case Normal ///< 正常状态
    case Pulling ///< 下拉状态
    case Refreshing ///< 刷新状态
}

class YFRefreshView: UIView {
    
    ///< 定义一个属性, 记录现在刷新view的刷新状态
    ///< 这里相当于一个set方法, 每当状态改变的时候, 就去重新设置label的文字等
    ///< 还需要重新设置指示imageView的动画
    var state: YFRefreshState = .Normal {
        didSet {
            switch state {
            case .Normal:
                ///< 需要停止菊花转动, 并且将tipImageView显示出来
                tipImageView?.isHidden = false
                indicator?.stopAnimating()
                tipLabel?.text = "需要加把劲继续拉..."
                UIView.animate(withDuration: 0.25) {
                    self.tipImageView?.transform = CGAffineTransform.identity
                }
            case .Pulling:
                tipLabel?.text = "可以放手了..."
                UIView.animate(withDuration: 0.25) {
                    ///< 这里减掉一个0.001是为了一个就近原则, 这样动画会按照最近的路程返回, 就不会出现一只顺时针的情况
                    self.tipImageView?.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi - 0.001))
                }
            case .Refreshing:
                tipLabel?.text = "正在刷新中, 请稍后"
                tipImageView?.isHidden = true
                indicator?.startAnimating()
            }
        }
    }
    
    ///< 指示器
    @IBOutlet weak var indicator: UIActivityIndicatorView?
    
    ///< 提示imageView
    @IBOutlet weak var tipImageView: UIImageView?
    
    ///< 提示文字
    @IBOutlet weak var tipLabel: UILabel?
    
    var pullingHeight: CGFloat = 0;
}

extension YFRefreshView {
    class func refreshView()->YFRefreshView {
        let nib = UINib(nibName: "YFWeiboRefreshView", bundle: nil)
        
        return nib.instantiate(withOwner: nil, options: nil)[0] as! YFRefreshView
    }
}
